/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sgsmcan.Seguridad;

import com.sgsmcan.Utils.Utilitario;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author user
 */
public class Filtro implements Filter{
    
    private final String CLASE ="[Filtro]"; 
    private FilterConfig config;
    private String urlLogin;
    
    @Override
    public void init(FilterConfig fc) throws ServletException {
         this.config = fc;

        //Tambien se pueden cargar los parametros
        //Configura url desconexión
        this.urlLogin = config.getInitParameter("urlLogin");
        if (urlLogin == null || urlLogin.trim().length() == 0) {
            Utilitario.LOG.debug(CLASE+" No se ha configurado la url de login");
            throw new ServletException("No se ha configurado URL de login");
        }
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (((HttpServletRequest) request).getSession().getAttribute(
                "usuarioBean") == null) {
            Utilitario.LOG.debug(CLASE+" es nulo");
            ((HttpServletResponse) response).sendRedirect("/SGSMCAN/faces/index.xhtml");
        } else {
            Utilitario.LOG.debug(CLASE+" No es nulo");
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
        config = null;
    }
    
}
